package com.xlongwei.info.hook;

import java.util.stream.Collectors;

import com.networknt.server.StartupHookProvider;
import com.networknt.service.SingletonServiceFactory;
import com.xlongwei.info.model.User;
import com.xlongwei.info.store.Store;
import com.xlongwei.info.util.InfoConfig;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Startup implements StartupHookProvider {

	@Override
	public void onStartup() {
		log.info("startup");
		loadConfig();
	}

	public static void loadConfig() {
		InfoConfig config = InfoConfig.INSTANCE;
		log.info("admin={} rawToken={}", config.getAdmin(), config.isRawToken());
		Store store = SingletonServiceFactory.getBean(Store.class);
		log.info("owners={}", config.getOwners().stream().map(User::getUser).collect(Collectors.toList()));
		config.getOwners().forEach(user -> {
			User owner = User.builder().user(user.getUser()).pass(user.getPass()).owner(true).build();
			User u = store.user(user.getUser());
			if (u != null) {
				owner.setPass(u.getPass());
				owner.setBan(u.isBan());
			}
			store.save(owner);
		});
	}

}
